package com.leetcode.week.week292;

/**
 * @author angzhijin
 * @Description
 * @create 2022-05-08 10:39 上午
 */
public class week2 {
    public static void main(String[] args) {

    }

    static int count =0;
    public static int averageOfSubtree(TreeNode root) {
        dfs(root);
        return count;
    }

    public static int[] dfs(TreeNode node){
        if(node==null) return new int[]{0,0};
        int[] left = dfs(node.left);
        int[] right = dfs(node.right);
        int sum = left[0]+right[0]+node.val;
        int cnt = left[1]+right[1]+1;
        if(sum/cnt == node.val) count++;

        return new int[]{sum,cnt};

    }

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
